package com.group3.controller.Validated;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;
import java.util.regex.Pattern;

/**
 * Phone
 *
 * @author HY
 * 2021/06/14 15:02
 **/

@Constraint(validatedBy = IdCardSelect.IdCardValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IdCardSelect {

    String message() default "身份证格式不合法";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class IdCardValidator implements ConstraintValidator<IdCardSelect, String> {

        private Pattern pattern = Pattern.compile("(^\\d{18}$)|(^\\d{15}$)");

        @Override
        public void initialize(IdCardSelect idCard) {
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            //判断是否为null
            if (value == null){
                return true;
            }
            //判断是否为空字符串
            if (value == ""){
                return true;
            }
            return pattern.matcher(value).matches();
        }
    }
}
